package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.ProductSkus;
import com.ruoyi.mall.domain.ProductSpecValues;
import com.ruoyi.mall.domain.Specifications;
import com.ruoyi.mall.mapper.GoodsMapper;
import com.ruoyi.mall.mapper.ProductSkusMapper;
import com.ruoyi.mall.service.IProductSkusService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品SKUController
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/mall/skus")
public class ProductSkusController extends BaseController {
    @Autowired
    private IProductSkusService productSkusService;
    @Autowired
    private ProductSkusMapper productSkusMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品SKU列表
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSkus productSkus) {
        startPage();
        List<ProductSkus> list = productSkusService.selectProductSkusList(productSkus);
        return getDataTable(list);
    }

    /**
     * 查询商品SKU列表
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:list')")
    @GetMapping("/byGoodIds")
    public TableDataInfo byGoodIds(Integer goodsId) {
        startPage();
        List<Specifications> specifications = goodsMapper.selSpec(goodsId);
        for (Specifications specification : specifications) {
            List<ProductSpecValues> productSpecValues = goodsMapper.selProd(specification.getSpecId().intValue());
            specification.setSpecValuesList(productSpecValues);
        }
        List<ProductSpecValues> productSpecValues = specifications.get(0).getSpecValuesList();
        List<ProductSpecValues> productSpecValues1 = specifications.get(1).getSpecValuesList();
        List<ProductSkus> skuses = new ArrayList<>();
        for (ProductSpecValues productSpecValue : productSpecValues) {
            for (ProductSpecValues specValues : productSpecValues1) {
                String sku_code = productSpecValue.getSpecValueId() + "-" + specValues.getSpecValueId();
                logger.info("sku_code=" + sku_code);
                ProductSkus productSkus = goodsMapper.selSkuCode(sku_code);
                productSkus.setSpecValue(productSpecValue.getSpecValueId().intValue());
                productSkus.setSpecValueName(productSpecValue.getSpecValue());
                productSkus.setSpecValue1(specValues.getSpecValueId().intValue());
                productSkus.setSpecValue1Name(specValues.getSpecValue());
                skuses.add(productSkus);
            }
        }
        return getDataTable(skuses);
    }

    /**
     * 导出商品SKU列表
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:export')")
    @Log(title = "商品SKU", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSkus productSkus) {
        List<ProductSkus> list = productSkusService.selectProductSkusList(productSkus);
        ExcelUtil<ProductSkus> util = new ExcelUtil<ProductSkus>(ProductSkus.class);
        util.exportExcel(response, list, "商品SKU数据");
    }

    /**
     * 获取商品SKU详细信息
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:query')")
    @GetMapping(value = "/{skuId}")
    public AjaxResult getInfo(@PathVariable("skuId") Integer skuId) {
        return success(productSkusService.selectProductSkusBySkuId(skuId.longValue()));
    }

    /**
     * 新增商品SKU
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:add')")
    @Log(title = "商品SKU", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSkus productSkus) {
        return toAjax(productSkusService.insertProductSkus(productSkus));
    }

    /**
     * 修改商品SKU
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:edit')")
    @Log(title = "商品SKU", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSkus productSkus) {
        return toAjax(productSkusService.updateProductSkus(productSkus));
    }

    /**
     * 删除商品SKU
     */
    @PreAuthorize("@ss.hasPermi('shangpin:ProductSkus:remove')")
    @Log(title = "商品SKU", businessType = BusinessType.DELETE)
    @DeleteMapping("/{skuIds}")
    public AjaxResult remove(@PathVariable Long[] skuIds) {
        return toAjax(productSkusService.deleteProductSkusBySkuIds(skuIds));
    }
}
